﻿using System;
using System.Collections.Generic;
using System.Linq;
using Drill.Configuration;
using Drill.TestSupport;
using Drill.TestSupport.TestDomains.SimpleDomain;
using Microsoft.Practices.ServiceLocation;
using NUnit.Framework;

namespace Drill.Integration.CommonServiceLocator
{
	[TestFixture]
	public class ServiceLocatorAdapterBackedByUnityDrillBitTests
    {
		private IDependencyResolver CreateDependencyResolver()
		{
			DependencyResolverConfiguration config = TestConfigs.CreateBasicDependencyResolverConfigurationWithUnityDrillBit();
			DependencyResolverManager.Reset();
			return DependencyResolverManager.Create(config);
		}

		private IServiceLocator CreateServiceLocatorAdapter()
		{
			return new ServiceLocatorAdapter(CreateDependencyResolver());
		}

		[Test]
		public void CanConstruct()
		{
			// Arrange
			IDependencyResolver dependencyResolver = CreateDependencyResolver();

			// Act
			ServiceLocatorAdapter serviceLocator = new ServiceLocatorAdapter(dependencyResolver);

			// Assert
			Assert.That(serviceLocator, Is.Not.Null);
		}

		[ExpectedException(typeof(ArgumentNullException), ExpectedMessage = "dependencyResolver", MatchType = MessageMatch.Contains)]
		[Test]
		public void CannotConstructWithNullDependencyResolverArgument()
		{
			// Arrange
			IDependencyResolver dependencyResolver = null;

			// Act
			// ReSharper disable ExpressionIsAlwaysNull
			// ReSharper disable ObjectCreationAsStatement
			new ServiceLocatorAdapter(dependencyResolver);
			// ReSharper restore ObjectCreationAsStatement
			// ReSharper restore ExpressionIsAlwaysNull

			// Assert
			// Nothing to assert because the test should throw an expected exception (see ExpectedException attribute).
		}

		[Test]
		public void CanGetService()
		{
			// Arrange
			IServiceLocator serviceLocator = CreateServiceLocatorAdapter();

			// Act
			ITestObjA obj = serviceLocator.GetService(typeof (ITestObjA)) as ITestObjA;

			// Assert
			Assert.That(obj, Is.Not.Null);
			Assert.That(obj, Is.AssignableTo(typeof(TestObjA)));
		}

		[Test]
		public void CanGetInstance()
		{
			// Arrange
			IServiceLocator serviceLocator = CreateServiceLocatorAdapter();

			// Act
			ITestObjA obj = serviceLocator.GetInstance(typeof(ITestObjA)) as ITestObjA;

			// Assert
			Assert.That(obj, Is.Not.Null);
			Assert.That(obj, Is.AssignableTo(typeof(TestObjA)));
		}

		[Test]
		public void CanGetNamedInstance()
		{
			// Arrange
			IServiceLocator serviceLocator = CreateServiceLocatorAdapter();

			// Act
			ITestObjB obj = serviceLocator.GetInstance(typeof(ITestObjB), "MyObjB") as ITestObjB;

			// Assert
			Assert.That(obj, Is.Not.Null);
			Assert.That(obj, Is.AssignableTo(typeof(ITestObjB)));
		}

		[Test]
		public void CanGetAllInstances()
		{
			// Arrange
			IServiceLocator serviceLocator = CreateServiceLocatorAdapter();

			// Act
			IEnumerable<object> enumerable = serviceLocator.GetAllInstances(typeof(ITestObjB));

			// Assert
			Assert.That(enumerable, Is.Not.Null);
			// ReSharper disable AssignNullToNotNullAttribute
			Assert.That(enumerable.Any(o => o is TestObjB), Is.True);
			// ReSharper restore AssignNullToNotNullAttribute
		}

		[Test]
		public void CanGetInstanceGeneric()
		{
			// Arrange
			IServiceLocator serviceLocator = CreateServiceLocatorAdapter();

			// Act
			ITestObjA obj = serviceLocator.GetInstance<ITestObjA>();

			// Assert
			Assert.That(obj, Is.Not.Null);
			Assert.That(obj, Is.AssignableTo(typeof(TestObjA)));
		}

		[Test]
		public void CanGetNamedInstanceGeneric()
		{
			// Arrange
			IServiceLocator serviceLocator = CreateServiceLocatorAdapter();

			// Act
			ITestObjB obj = serviceLocator.GetInstance<ITestObjB>("MyObjB");

			// Assert
			Assert.That(obj, Is.Not.Null);
			Assert.That(obj, Is.AssignableTo(typeof(ITestObjB)));
		}

		[Test]
		public void CanGetAllInstancesGeneric()
		{
			// Arrange
			IServiceLocator serviceLocator = CreateServiceLocatorAdapter();

			// Act
			IEnumerable<ITestObjB> enumerable = serviceLocator.GetAllInstances<ITestObjB>();

			// Assert
			Assert.That(enumerable, Is.Not.Null);
			// ReSharper disable AssignNullToNotNullAttribute
			Assert.That(enumerable.Any(o => o is TestObjB), Is.True);
			// ReSharper restore AssignNullToNotNullAttribute
		}
    }
}
